<?php

namespace Admin\Controller;
use Think\Controller;

class AppbankController extends Controller{

    //列表

     public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');
        $page_size = I('post.page_size/d');
      
        $statustype = I('post.statustype');
   



        $model2 = M('gametype');
        $model1 = M('gameinfo');

        if($statustype==5){

            $count = $model1->join('table_gametype  on table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isborrow=1 || table_gametype.ismanagemoney=1 ')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
     
            $result = $model1->join('table_gametype  on table_gametype.gameid=table_gameinfo.id')->order('table_gametype.gameid desc')->where('table_gametype  on table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isborrow=1 || table_gametype.ismanagemoney=1 ')->limit($limitStart,$page_size)->select();
            $data['sele'] = 1;

    }


    if($statustype=="0"){
            $count = $model2->where('isborrow=1')->count();

            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isborrow=1')->order('table_gametype.borrowrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="1"){
            $count = $model2->where('ismanagemoney=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.ismanagemoney=1')->order('table_gametype.managemoneyrank desc')->limit($limitStart,$page_size)->select();
        }elseif ($statustype=="4") {
            $count = $model2->where('isbank=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 1;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isbank=1')->order('table_gametype.gameid desc')->limit($limitStart,$page_size)->select();
        }


        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

	
	/*
					**************************游戏页面app相关方法 *****************************
	*/

	/*游戏平台分类渲染*/
	public  function  gamexuanran(){
		 //$this->isAjaxLogin();
        $result = array
                      (
                        array(
                             'id'=>'0','name'=>'借款'
                          ),
                        array('id'=>'1','name'=>'理财'
                        )
                     
                      );

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}				

	/*添加游戏平台用户*/

	public function gameadd(){

		$game = M('gameinfo');
		$game1 = M('gametype');

		$name = I('post.name');
		$picture = I('post.picture');
		$screenshot = I('post.screenshot');
		$screenshot=implode(',', $screenshot);
		$developer=I('post.developer');
		$apk_size = I('post.apk_size');
		$apk = I('post.apk');//apk链接
		$versioninfo = I('post.versioninfo');//
		$comment=I('post.comment');
		$introduce=I('post.introduce');
		$people=I('post.people');
		$apkname=I('post.apkname');//

        $apptype1=I('post.apptype1');
        $apptype2=I('post.apptype2');
        $apptype3=I('post.apptype3');
        $apptype4=I('post.apptype4');
		$istuijian=I('post.istuijian');

		$data['name'] = $name;
		$data['picture'] = $picture;
		$data['screenshot'] = $screenshot;
		$data['developer'] = $developer;
		$data['apk_size'] = $apk_size;
		$data['apk'] = $apk;
		$data['versioninfo'] = $versioninfo;
		$data['comment'] = $comment;
		$data['introduce'] = $introduce;
		$data['people'] = $people;
		$data['apkname'] = $apkname;
		$data['isshow'] = 0;

		$data1['isbank'] = $istuijian;
		
		
			$data1['isborrow']=$apptype1;
		
			$data1['ismanagemoney']=$apptype2;
		
			
		
		

		$result = $game->add($data);

            $data1['borrowrank'] = $result;
            $data1['managemoneyrank'] =  $result;
           

		

		$result1 = $game1->add($data1);

		if($result!=false && $result1!=false){

			$ajaxReturn['code'] = 1;
			$ajaxReturn['msg'] = '添加成功！';

		}else{

			$ajaxReturn['code'] = 2;
			$ajaxReturn['msg'] = "添加失败！";

		}

		$this->ajaxReturn($ajaxReturn);
	}


    /*修改渲染*/
    public  function  editxiugai(){
        $id=I('post.id');
        $model=M('gameinfo');
        $result=$model->join('table_gametype on  table_gameinfo.id=table_gametype.gameid')->where('table_gameinfo.id='.$id)->select();

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = '添加成功！';
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "添加失败！";

        }

        $this->ajaxReturn($ajaxReturn);
    }


    /*修改*/
    public function gameedit(){

        $game = M('gameinfo');
        $game1 = M('gametype');

        $id = I('post.id');
        $name = I('post.name');
        $picture = I('post.picture');
        $screenshot = I('post.screenshot');
        $screenshot=implode(',', $screenshot);
        $developer=I('post.developer');
        $apk_size = I('post.apk_size');
        $apk = I('post.apk');//apk链接
        $versioninfo = I('post.versioninfo');//
        $comment=I('post.comment');
        $introduce=I('post.introduce');
        $people=I('post.people');
        $apkname=I('post.apkname');//app标签

        $istuijian=I('post.istuijian');
       
        $apptype1=I('post.apptype1');
        $apptype2=I('post.apptype2');
        
        $istuijian=I('post.istuijian');


        $data['name'] = $name;
        $data['picture'] = $picture;
        $data['screenshot'] = $screenshot;
        $data['developer'] = $developer;
        $data['apk_size'] = $apk_size;
        $data['apk'] = $apk;
        $data['versioninfo'] = $versioninfo;
        $data['comment'] = $comment;
        $data['introduce'] = $introduce;
        $data['people'] = $people;
        $data['apkname'] = $apkname;
        $data['isshow'] = 0;

        $data1['isbank'] = $istuijian;
        
            $data1['isborrow']=$apptype1;
        
            $data1['ismanagemoney']=$apptype2;
        
        

        $result = $game->save($data);
        

        $result1 = $game1->save($data1);

        if($result!=false && $result1!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = '添加成功！';

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "添加失败！";

        }

        $this->ajaxReturn($ajaxReturn);
    }



	/*游戏上下架*/

	public function isshow(){

		$isshow =I('post.isshow');//1上架，0下架
		$id = I('post.id');

		$data['isshow']=$isshow;

		$game = M('gameinfo');

		$result = $game->where('id='.$id)->save($data);

		if($result!=false){

			$ajaxReturn['code'] = 1;
			$ajaxReturn['msg'] = '添加成功！';
			$ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 2;
			$ajaxReturn['msg'] = "添加失败！";

		}

		$this->ajaxReturn($ajaxReturn);
	}

	/*游戏页面的列表展示*/

	public function gamelist(){
        $page =I('post.page/d');
        $page_size = I('post.page_size/d');
		$statustype=I('post.statustype');
		$model1=M('gameinfo');
		$model2=M('gametype');

		if($statustype=="0"){
            $count = $model2->where('isborrow=1')->count();

            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isborrow=1')->order('table_gametype.borrowrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="1"){
            $count = $model2->where('ismanagemoney=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.ismanagemoney=1')->order('table_gametype.managemoneyrank desc')->limit($limitStart,$page_size)->select();
        }elseif ($statustype=="4") {
            $count = $model2->where('isgame=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 1;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isgame=1')->order('table_gametype.gameid desc')->limit($limitStart,$page_size)->select();
        }

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	/*
     * 排序
     */
    public function gamerank (){

        $apptype=I('post.statustype');

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('gametype');



        if($apptype=="0"){
            $thisinfo   = $ArticleModel->where('gameid='.$thisid)->getField('borrowrank,ishotgame');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('borrowrank'.$and.$thisrank)->order('borrowrank '.$order)->getField('gameid,borrowrank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];



            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('gameid='.$thisid)->setField('borrowrank',$nextrank);
                $ArticleModel-> where('gameid='.$nextid)->setField('borrowrank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }elseif ($apptype=="1") {
            $thisinfo   = $ArticleModel->where('gameid='.$thisid)->getField('managemoneyrank,ishotgame');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('managemoneyrank'.$and.$thisrank)->order('managemoneyrank '.$order)->getField('gameid,managemoneyrank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];

            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('gameid='.$thisid)->setField('managemoneyrank',$nextrank);
                $ArticleModel-> where('gameid='.$nextid)->setField('managemoneyrank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }

        

        $this->ajaxReturn($ajaxReturn);

    }

     public  function  gamedelete(){
        $id=I('post.id');
        $model=M('gameinfo');
        $model1=M('gametype');


        $model->where('id='.$id)->delete();
        $model1->where('gameid='.$id)->delete();

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);
    }
}